package com.banmao.desktoptools.baseView;

import javafx.scene.control.Alert;
import javafx.scene.control.ButtonBar;
import javafx.scene.control.ButtonType;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.Map;
import java.util.function.Predicate;

/**
 *  弹出提示框
 */
@Data
public class Message {

    // 标题
    private String title;
    // 信息
    private String message;
    // 头部
    private String headerText;
    // 提示类型
    private Alert.AlertType alertType = Alert.AlertType.CONFIRMATION;
    // 自定义按钮
    private ArrayList<ButtonType> buttonTypeList;
    // 处理方法
    private Map<ButtonBar.ButtonData, Predicate> handleButton;

    public Message() {}

    public Message(String title, String message) {
        this.title = title;
        this.message = message;
    }

    public Message(String title, String message, Alert.AlertType alertType) {
        this.title = title;
        this.message = message;
        this.headerText = headerText;
    }

    public Message(String title, String message, Alert.AlertType alertType, String headerText) {
        this.title = title;
        this.message = message;
        this.headerText = headerText;
        this.alertType = alertType;
    }

/*public static void comfirm() {
        Alert alert = new Alert(Alert.AlertType.CONFIRMATION, "测试");
        // 窗口标题
        alert.setTitle("确认");
        alert.setHeaderText("头部");
        // 设置对话框 icon 图标，参数是主窗口的stage
        alert.initOwner(DesktopToolsApplication.getStage());

        // showAndWait 在对话框消失前不会继续执行代码
        Optional<ButtonType> buttonType = alert.showAndWait();
        // 根据点击结果返回
        if(buttonType.get().getButtonData().equals(ButtonBar.ButtonData.YES)) {
        } else {
        }
    }*/

    private static Alert getAlert(Alert.AlertType alertType, String title, String message,
                                  String headerText, ButtonType[] buttonTypes) {
        Alert alert;
        if(buttonTypes == null || buttonTypes.length == 0) {
            alert = new Alert(alertType, message);
        } else {
            alert = new Alert(alertType, message, buttonTypes);
        }
        alert.setTitle(StringUtils.isBlank(title) ? "确认" : title);
        if(StringUtils.isNotBlank(headerText)) {
            alert.setHeaderText(headerText);
        }
        return alert;
    }

    // 确认弹框
    public void confirmAndWait() {

    }

/*
    public static void confirmAndWait(String title, String message, String headerText) {
        //Alert alert = getAlert(Alert.AlertType.CONFIRMATION, title, message, headerText);
        confirmAndWait(title, message, headerText, null);

    }

    private static void confirmAndWait(String title, String message, String headerText, ButtonType[] buttonTypes) {
        //Alert alert = getAlert(Alert.AlertType.CONFIRMATION, title, message, headerText, buttonTypes);
        showAndWait(Alert.AlertType.CONFIRMATION, title, message, headerText, buttonTypes);
    }*/

    public void showAndWait() {
        Alert alert = getAlert(this.alertType, this.title, this.message, this.headerText, null);
        alert.showAndWait();
    }

    public void show(Long secs) throws InterruptedException {
        Alert alert = getAlert(this.alertType, this.title, this.message, this.headerText, null);
        alert.show();
        Thread.sleep(secs);
        alert.close();
    }

    public void show() throws InterruptedException {
        Alert alert = getAlert(this.alertType, this.title, this.message, this.headerText, null);
        alert.show();
    }

}
